//
//  AppDelegate.m
//  Qyer
//
//  Created by 赵宇 on 15/9/8.
//  Copyright (c) 2015年 赵宇. All rights reserved.
//

#import "AppDelegate.h"
#import "BaseHeader.h"
@interface AppDelegate ()

@end

@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.window setBackgroundColor:[UIColor whiteColor]];
    [self.window makeKeyAndVisible];
 
    [self creatTabbar];
    
    [self userGuide];
    [self NightAndDay];

    // 友盟
//    [UMSocialData setAppKey:@"507fcab25270157b37000010"];
//    [_window release];
    
    return YES;
}
#pragma mark - 创建tabbar
-(void)creatTabbar
{
    RecommendViewController *recommend = [[RecommendViewController alloc]init];
    UINavigationController *recommendNav = [[UINavigationController alloc]initWithRootViewController:recommend];
    recommendNav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"推荐" image:[UIImage imageNamed:@"iconfont-tuijian.png"] tag:1000];
    
    DestinationViewController *destination = [[DestinationViewController alloc]init];
    UINavigationController *destinationNav = [[UINavigationController alloc]initWithRootViewController:destination];
    destinationNav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"目的地" image:[UIImage imageNamed:@"iconfont.png"] tag:1001];
    
    MyViewController *my = [[MyViewController alloc]init];
    UINavigationController *myNav = [[UINavigationController alloc]initWithRootViewController:my];
    myNav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"contact.png"] tag:1003];
    
    UITabBarController *tabbarController = [[UITabBarController alloc]init];
    tabbarController.viewControllers = [NSArray arrayWithObjects:recommendNav,destinationNav,myNav, nil];
    tabbarController.tabBar.tintColor = [UIColor colorWithRed:34./255 green:169./255 blue:118./255 alpha:1];
    [[DataBase shareDataBase]openDB];
    [[DataBase shareDataBase]createTable];
    
    [self.window setRootViewController:tabbarController];
    [recommend release];
    [recommendNav release];
    [destination release];
    [destinationNav release];
    [my release];
    [myNav release];
}
-(void)userGuide
{
    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"firstLaunch"] == NO) {
        
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"firstLaunch"];
        // 第一次启动操作,比如新下的一个App,有4个图,然后开启神马之旅,等二次就不执行了
      self.scView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [self.scView setPagingEnabled:YES];
        [self.scView setBounces:NO];
        [self.scView setContentSize:CGSizeMake(SCREEN_WIDTH*4, 0)];
        [self.scView showsHorizontalScrollIndicator];
        
        for (int i= 0; i< 4; i++) {
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            [imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"userGuide%d.png",i]]];
            [self.scView addSubview:imageView];
        }
        [self.window addSubview:self.scView];
        
        UIButton *bu = [UIButton buttonWithType:UIButtonTypeCustom];
        
        bu.titleLabel.textAlignment = YES;
        bu.frame = CGRectMake(SCREEN_WIDTH*3.3, 0.75*SCREEN_HEIGHT, SCREEN_WIDTH/3, 0.08*SCREEN_HEIGHT);
        bu.backgroundColor = [UIColor colorWithRed:23./255 green:101./255 blue:74./255 alpha:1];
        bu.layer.borderWidth = 1.;
        bu.layer.borderColor = [[UIColor whiteColor]CGColor];
        bu.layer.cornerRadius = 15;
        [bu setTitle:@"开启奇幻之旅" forState:UIControlStateNormal];
        [bu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        bu.titleLabel.font = [UIFont systemFontOfSize:16.];
        [bu addTarget:self action:@selector(button) forControlEvents:UIControlEventTouchUpInside];
        bu.center = CGPointMake(SCREEN_WIDTH*3.5, 0.85*SCREEN_HEIGHT);
        [self.scView addSubview:bu];
        
    }
}
-(void)button
{
    [self.window setUserInteractionEnabled:YES];
    
    [UIView animateWithDuration:3 animations:^{
        
        self.scView.alpha = 0;
        
        
    }];

}
#pragma mark -- 夜间模式
-(void)NightAndDay
{
    // 消息中心
    // 属于观察者模式,全工程都能使用
    // 注册消息,只要接收到name为Night的post信息,就会调用方法
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(HelloNight) name:@"Night" object:nil];
    // 注册消息,只要接收到name为Day的post信息,就会调用方法
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(HelloDay) name:@"Day" object:nil];
}
-(void)HelloNight
{
    self.window.alpha = 0.6;
}
-(void)HelloDay
{
    self.window.alpha = 1;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
